package com.example.demoweb.controller;

import com.example.demoweb.entity.Person;
import com.example.demoweb.enums.Gender;
import com.example.demoweb.enums.TimeType;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 赵恒
 * @date 2023/5/22
 */
@RestController
@RequestMapping("/person")
@Slf4j
@Api(tags = "person类")
public class PersonController {
    ObjectMapper objectMapper = new ObjectMapper();

    @ApiOperation("get-Date")
    @GetMapping("/onlyGet")
    public Map<String, Object> getPerson(Integer id, String name, @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday) {
        log.info("id:{}, name:{}, birthday:{}", id, name, birthday);
        Map<String, Object> result = new HashMap<>();
        result.put("id", id);
        result.put("name", name);
        result.put("birthday", birthday);
        return result;
    }

    @ApiOperation("get-Person")
    @GetMapping("/classGet")
    public Person classGetPerson(Person person) {
        try {
            log.info(objectMapper.writeValueAsString(person));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return person;
    }

    @ApiOperation("get-Enum")
    @GetMapping("/enumGet")
    public Map<String, Object> getEnum(Integer id, TimeType timeType) {
        log.info("id:{}, timeType:{}", id, timeType);
        Map<String, Object> result = new HashMap<>();
        result.put("id", id);
        result.put("timeType", timeType);
        return result;
    }

    @ApiOperation("get-Date-format")
    @GetMapping("/localdateTimeGet")
    public Map<String, Object> getDateTime(Integer id, @DateTimeFormat(pattern = "yyyy-MM-dd") Date createTime) {
        log.info("id:{}, createTime:{}", id, createTime);
        Map<String, Object> result = new HashMap<>();
        result.put("id", id);
        result.put("createTime", createTime);
        return result;
    }

    @ApiOperation("get-Enum-Value")
    @GetMapping("/enumParamGet")
    public Map<String, Object> getEnumParam(Integer id, Gender gender) {
        log.info("id:{}, gender:{}", id, gender);
        Map<String, Object> result = new HashMap<>();
        result.put("id", id);
        result.put("gender", gender);
        return result;
    }

    @ApiOperation("post-Person-nobody")
    @PostMapping("/onlyPost")
    public Person postPerson(Person person) {
        try {
            log.info(objectMapper.writeValueAsString(person));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return person;
    }

    @ApiOperation("post-Person-Body")
    @PostMapping("/bodyPost")
    public Person bodyPostPerson(@RequestBody Person person) {
        try {
            log.info(objectMapper.writeValueAsString(person));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return person;
    }

}
